package com.ttxz.sample

import androidx.lifecycle.Observer
import com.ttxz.common.http.*

abstract class IHttpResponseObserver<T> : Observer<HttpResponse<T>> {

    override fun onChanged(apiResponse: HttpResponse<T>) {
        when (apiResponse) {
            is HttpSuccessResponse -> onSuccess(apiResponse.response)
            is HttpEmptyResponse -> onDataEmpty()
            is HttpDataFailedResponse -> onFailed(apiResponse.errorCode, apiResponse.errorMsg)
            is HttpErrorResponse -> onError(apiResponse.throwable)
        }
        onComplete()
    }

    abstract fun onComplete()

    abstract fun onError(throwable: Throwable)

    abstract fun onFailed(errorCode: Int, errorMsg: String?)

    abstract fun onDataEmpty()

    abstract fun onSuccess(response: T?)

}